import md5 from 'js-md5'
import OSS from 'ali-oss'

// const dev_url='ost-app-img-dev';
// const devideo_url='ost-app-video-dev';
// const devcontract='all-contract-dev';
const dev_url = 'ost-app-img'
const devideo_url = 'ost-app-video'
const devcontract = 'all-contract'
const devfile = 'starcat-backend-file'

export default {
  upload_img: (file, success) => {
    console.log('file', file)
    let arr = {
      img_url: '',
      percentage: '',
      wjlj: '',
    }

    let fileName = md5(file.name)
    let client = new OSS({
      region: 'oss-cn-beijing',
      accessKeyId: 'LTAI4GETVhjW9o7BMnChqVpk',
      accessKeySecret: 'ja5ArjSU3yFfiyVM2AXdfgmRwjeXEu',
      bucket: devcontract,
    })
    let timestamp = Date.parse(new Date())
    timestamp = timestamp / 1000
    let n = timestamp * 1000
    let date = new Date(n)
    let dates = date.toJSON().substr(0, 10)
    let str = '/starcat/' + dates.replace(/-/g, '')
    // 添加文件后缀名
    let jptype = file.name.substr(file.name.lastIndexOf('.')).toLowerCase()
    let aliyunFileKey = str + '/' + fileName + Date.parse(new Date()) + jptype
    return client
      .multipartUpload(aliyunFileKey, file.raw, {
        progress: function (percentage) {
          arr.percentage = Math.floor(percentage * 100)
          success(arr)
        },
      })
      .then(function (res) {
        arr.img_url =
          'https://' +
          devcontract +
          '.oss-cn-beijing.aliyuncs.com' +
          aliyunFileKey
        success(arr)
      })
      .catch((err) => {
        console.log(err)
        // this.$message.warning('阿里云上传失败，请联系工作人员');
      })
  },
  upload_txt: (file, success) => {
    console.log(file)
    let arr = {
      img_url: '',
      percentage: '',
    }
    let fileName = md5(file.name)
    let client = new OSS({
      region: 'oss-cn-beijing',
      accessKeyId: 'LTAI4GETVhjW9o7BMnChqVpk',
      accessKeySecret: 'ja5ArjSU3yFfiyVM2AXdfgmRwjeXEu',
      bucket: devcontract,
    })
    let timestamp = Date.parse(new Date())
    timestamp = timestamp / 1000
    let n = timestamp * 1000
    let date = new Date(n)
    let dates = date.toJSON().substr(0, 10)
    let str = dates.replace(/-/g, '')
    // 添加文件后缀名
    let aliyunFileKey = str + '/' + file.name
    return client
      .multipartUpload(aliyunFileKey, file.raw, {
        progress: function (percentage) {
          arr.percentage = Math.floor(percentage * 100)
          success(arr)
        },
      })
      .then(function (res) {
        arr.img_url =
          'https://' +
          devcontract +
          '.oss-cn-beijing.aliyuncs.com/' +
          aliyunFileKey
        success(arr)
      })
      .catch((err) => {
        console.log(err)
      })
  },
  upload_video: (file, success) => {
    // console.log(file)
    let arr = {
      video_url: '',
      video_imgurl: '',
      percentage: null,
    }

    let client = new OSS({
      region: 'oss-cn-beijing',
      accessKeyId: 'LTAI4GETVhjW9o7BMnChqVpk',
      accessKeySecret: 'ja5ArjSU3yFfiyVM2AXdfgmRwjeXEu',
      bucket: devideo_url,
    })
    let fileName = md5(file.name)
    let timestamp = Date.parse(new Date())
    timestamp = timestamp / 1000
    let n = timestamp * 1000
    let date = new Date(n)
    let dates = date.toJSON().substr(0, 10)
    let str = dates.replace(/-/g, '')
    // let str = "/starcat/"+dates.replace(/-/g, "");
    // 添加文件后缀名
    // let jptype = file.name.substr(file.name.indexOf(".") + 1);
    let jptype = file.name.substr(file.name.lastIndexOf('.')).toLowerCase()
    // console.log(suffixname,"jptypejptype")
    let allname = fileName + Date.parse(new Date()) + jptype
    let aliyunFileKey = 'basefile' + '/' + allname
    let aliyunFileKey1 = 'transcoding' + '/' + allname
    return client
      .multipartUpload(aliyunFileKey, file.raw, {
        progress: function (percentage) {
          arr.percentage = Math.floor(percentage * 100)
          success(arr)
        },
      })
      .then(function (res) {
        arr.video_imgurl =
          res.res.requestUrls[0].split('?')[0] +
          '?x-oss-process=video/snapshot,t_1,f_jpg,m_fast,ar_auto'
        arr.video_url =
          'https://' +
          devideo_url +
          '.oss-cn-beijing.aliyuncs.com/' +
          aliyunFileKey
        arr.video_urlzm =
          'https://' +
          devideo_url +
          '.oss-cn-beijing.aliyuncs.com/' +
          aliyunFileKey1
        success(arr)
      })
      .catch((err) => {
        console.log(err)
      })
  },
  elupload_video: (file, success) => {
    // console.log(file)
    let arr = {
      video_url: '',
      video_imgurl: '',
      percentage: null,
    }

    let client = new OSS({
      region: 'oss-cn-beijing',
      accessKeyId: 'LTAI4GETVhjW9o7BMnChqVpk',
      accessKeySecret: 'ja5ArjSU3yFfiyVM2AXdfgmRwjeXEu',
      // bucket: 'ost-app-video'
      bucket: devideo_url,
    })
    let fileName = md5(file.name)
    let timestamp = Date.parse(new Date())
    timestamp = timestamp / 1000
    let n = timestamp * 1000
    let date = new Date(n)
    let dates = date.toJSON().substr(0, 10)
    let str = dates.replace(/-/g, '')
    // let str = "/starcat/"+dates.replace(/-/g, "");
    // 添加文件后缀名
    // let jptype = file.name.substr(file.name.indexOf(".") + 1);
    let jptype = file.name.substr(file.name.lastIndexOf('.')).toLowerCase()
    // console.log(suffixname,"jptypejptype")
    let allname = fileName + Date.parse(new Date()) + jptype
    let aliyunFileKey = 'basefile' + '/' + allname
    let aliyunFileKey1 = 'transcoding' + '/' + allname
    return client
      .multipartUpload(aliyunFileKey, file, {
        progress: function (percentage) {
          arr.percentage = Math.floor(percentage * 100)
          success(arr)
        },
      })
      .then(function (res) {
        arr.video_imgurl =
          res.res.requestUrls[0].split('?')[0] +
          '?x-oss-process=video/snapshot,t_1,f_jpg,m_fast,ar_auto'
        arr.video_url =
          'https://' +
          devideo_url +
          '.oss-cn-beijing.aliyuncs.com/' +
          aliyunFileKey
        arr.video_urlzm =
          'https://' +
          devideo_url +
          '.oss-cn-beijing.aliyuncs.com/' +
          aliyunFileKey1
        success(arr)
      })
      .catch((err) => {
        console.log(err)
      })
  },
  upload_img_: (file, success) => {
    console.log('file', file)
    let arr = {
      img_url: '',
      percentage: '',
      wjlj: '',
    }
    let fileName = md5(file.name)
    let client = new OSS({
      region: 'oss-cn-beijing',
      accessKeyId: 'LTAI4GETVhjW9o7BMnChqVpk',
      accessKeySecret: 'ja5ArjSU3yFfiyVM2AXdfgmRwjeXEu',
      bucket: dev_url,
    })
    let timestamp = Date.parse(new Date())
    timestamp = timestamp / 1000
    let n = timestamp * 1000
    let date = new Date(n)
    let dates = date.toJSON().substr(0, 10)
    let str = '/starcat/' + dates.replace(/-/g, '')
    // 添加文件后缀名
    // let jptype = file.name.substr(file.name.indexOf(".") + 1);
    let jptype = file.name.substr(file.name.lastIndexOf('.')).toLowerCase()
    let aliyunFileKey = str + '/' + fileName + Date.parse(new Date()) + jptype
    return client
      .multipartUpload(aliyunFileKey, file, {
        progress: function (percentage) {
          arr.percentage = Math.floor(percentage * 100)
          success(arr)
        },
      })
      .then(function (res) {
        arr.img_url =
          'https://' + dev_url + '.oss-cn-beijing.aliyuncs.com' + aliyunFileKey
        success(arr)
      })
      .catch((err) => {
        console.log(err)
        // this.$message.warning('阿里云上传失败，请联系工作人员');
      })
  },
  upload_file: (file, success) => {
    let arr = {
      file_url: '',
      percentage: '',
      wjlj: '',
    }
    let fileName = md5(file.name)
    let client = new OSS({
      region: 'oss-cn-beijing',
      accessKeyId: 'LTAI4GETVhjW9o7BMnChqVpk',
      accessKeySecret: 'ja5ArjSU3yFfiyVM2AXdfgmRwjeXEu',
      bucket: devfile,
    })
    let timestamp = Date.parse(new Date())
    timestamp = timestamp / 1000
    let n = timestamp * 1000
    let date = new Date(n)
    let dates = date.toJSON().substr(0, 10)
    let str = dates.replace(/-/g, '')
    // 添加文件后缀名
    let jptype = file.name.substr(file.name.lastIndexOf('.')).toLowerCase()
    let aliyunFileKey = str + '/' + fileName + Date.parse(new Date()) + jptype
    return client
      .multipartUpload(aliyunFileKey, file.raw, {
        progress: function (percentage) {
          arr.percentage = Math.floor(percentage * 100)
          success(arr)
        },
      })
      .then(function (res) {
        arr.file_url =
          'https://' + devfile + '.oss-cn-beijing.aliyuncs.com/' + aliyunFileKey
        success(arr)
      })
      .catch((err) => {
        console.log(err)
        // this.$message.warning('阿里云上传失败，请联系工作人员');
      })
  },
  upload_cropper: (file, success) => {
    console.log('file', 6666666)
    let arr = {
      img_url: '',
      percentage: '',
      wjlj: '',
    }
    let fileName = md5(file.name)
    let client = new OSS({
      region: 'oss-cn-beijing',
      accessKeyId: 'LTAI4GETVhjW9o7BMnChqVpk',
      accessKeySecret: 'ja5ArjSU3yFfiyVM2AXdfgmRwjeXEu',
      bucket: devcontract,
    })
    let timestamp = Date.parse(new Date())
    timestamp = timestamp / 1000
    let n = timestamp * 1000
    let date = new Date(n)
    let dates = date.toJSON().substr(0, 10)
    let str = '/starcat/' + dates.replace(/-/g, '')
    // 添加文件后缀名
    let jptype = file.name.substr(file.name.lastIndexOf('.')).toLowerCase()
    let aliyunFileKey = str + '/' + fileName + Date.parse(new Date()) + jptype
    return client
      .multipartUpload(aliyunFileKey, file, {
        progress: function (percentage) {
          arr.percentage = Math.floor(percentage * 100)
          success(arr)
        },
      })
      .then(function (res) {
        arr.img_url =
          'https://' +
          devcontract +
          '.oss-cn-beijing.aliyuncs.com' +
          aliyunFileKey
        success(arr)
        console.log(arr, 787878)
      })
      .catch((err) => {
        console.log(err)
        // this.$message.warning('阿里云上传失败，请联系工作人员');
      })
  },
  elupload_file: (file, success) => {
    // console.log(file)
    let arr = {
      file_url: '',
    }
    let client = new OSS({
      region: 'oss-cn-beijing',
      accessKeyId: 'LTAI4GETVhjW9o7BMnChqVpk',
      accessKeySecret: 'ja5ArjSU3yFfiyVM2AXdfgmRwjeXEu',
      bucket: devfile,
    })
    let fileName = md5(file.name)
    let timestamp = Date.parse(new Date())
    timestamp = timestamp / 1000
    let n = timestamp * 1000
    let date = new Date(n)
    let dates = date.toJSON().substr(0, 10)
    let str = dates.replace(/-/g, '')
    // let str = "/starcat/"+dates.replace(/-/g, "");
    // 添加文件后缀名
    // let jptype = file.name.substr(file.name.indexOf(".") + 1);
    let jptype = file.name.substr(file.name.lastIndexOf('.')).toLowerCase()
    // console.log(suffixname,"jptypejptype")
    let allname = fileName + Date.parse(new Date()) + jptype
    let aliyunFileKey = 'basefile' + '/' + allname
    return client
      .multipartUpload(aliyunFileKey, file, {
        progress: function (percentage) {
          arr.percentage = Math.floor(percentage * 100)
          success(arr)
        },
      })
      .then(function (res) {
        arr.file_url =
          'https://' + devfile + '.oss-cn-beijing.aliyuncs.com/' + aliyunFileKey
        success(arr)
      })
      .catch((err) => {
        console.log(err)
      })
  },
  adupload_video: (file, success) => {
    // console.log(file)
    let arr = {
      video_url: '',
      video_imgurl: '',
      percentage: null,
    }

    let client = new OSS({
      region: 'oss-cn-beijing',
      accessKeyId: 'LTAI4GETVhjW9o7BMnChqVpk',
      accessKeySecret: 'ja5ArjSU3yFfiyVM2AXdfgmRwjeXEu',
      // bucket: 'ost-app-video'
      bucket: devideo_url,
    })
    let fileName = md5(file.name)
    let timestamp = Date.parse(new Date())
    timestamp = timestamp / 1000
    let n = timestamp * 1000
    let date = new Date(n)
    let dates = date.toJSON().substr(0, 10)
    let str = dates.replace(/-/g, '')
    // let str = "/starcat/"+dates.replace(/-/g, "");
    // 添加文件后缀名
    // let jptype = file.name.substr(file.name.indexOf(".") + 1);
    let jptype = file.name.substr(file.name.lastIndexOf('.')).toLowerCase()
    // console.log(suffixname,"jptypejptype")
    let allname = fileName + Date.parse(new Date()) + jptype
    let aliyunFileKey = 'basefile' + '/' + allname
    let aliyunFileKey1 = 'transcoding' + '/' + allname
    return client
      .multipartUpload(aliyunFileKey, file.raw, {
        progress: function (percentage) {
          arr.percentage = Math.floor(percentage * 100)
          success(arr)
        },
      })
      .then(function (res) {
        arr.video_imgurl =
          res.res.requestUrls[0].split('?')[0] +
          '?x-oss-process=video/snapshot,t_1,f_jpg,m_fast,ar_auto'
        arr.video_url =
          'https://' +
          devideo_url +
          '.oss-cn-beijing.aliyuncs.com/' +
          aliyunFileKey
        arr.video_urlzm =
          'https://' +
          devideo_url +
          '.oss-cn-beijing.aliyuncs.com/' +
          aliyunFileKey1
        success(arr)
      })
      .catch((err) => {
        console.log(err)
      })
  },
}
